package com.njlime.cms.service.app;

import com.njlime.cms.entities.Information;
import com.njlime.cms.entities.InformationComment;
import com.njlime.cms.entities.InformationMotion;
import com.njlime.cms.repository.app.AppInformationDao;
import com.njlime.cms.repository.app.AppInformationMotionDao;
import com.njlime.core.exception.BusinessException;
import com.njlime.core.helper.GlobalValue;
import com.njlime.core.helper.PageBean;
import com.njlime.member.entiies.MemberComment;
import com.njlime.member.service.app.AppMemberCollectionService;
import com.njlime.member.service.app.AppMemberCommentService;
import com.njlime.member.service.app.AppMemberFocusService;
import com.njlime.sys.service.UnitService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Dean on 2017/10/10.
 */

@Service
public class AppInformationService {
    @Resource
    AppInformationDao appInformationDao;
    @Resource
    AppInformationCommentService appInformationCommentService;
    @Resource
    AppMemberCollectionService appMemberCollectionService;
    @Resource
    AppInformationUpService appInformationUpService;
    @Resource
    AppMemberCommentService appMemberCommentService;
    @Resource
    AppInformationMotionService appInformationMotionService;
    @Resource
    UnitService unitService;
    @Resource
    AppMemberFocusService appMemberFocusService;

    /**
     * 详情
     * @param id
     * @return
     */
    public Information findOne(int id){
        return appInformationDao.findOne(id);
    }

    /**
     * 获取资讯信息列表 banner 列表
     *
     * @return
     */
    public List<Map<String, Object>> bannerList(Information information, int root_unit_id) {
        return appInformationDao.bannerList(information, root_unit_id);
    }

    /**
     * 获取专题列表
     *
     * @return
     */
    public PageBean topicList(PageBean pageBean, Information information, int root_unit_id) {
        return appInformationDao.topicList(pageBean, information, root_unit_id);
    }

    /**
     * 获取资讯信息列表非 banner 列表
     *
     * @return
     */
    public PageBean infoList(PageBean pageBean, Information information, Integer user_id, int root_unit_id) {
        return appInformationDao.infoList(pageBean, information, user_id, root_unit_id);
    }

    /**
     * 获取资讯信息列表 置顶列表
     *
     * @return
     */
    public List<Map<String, Object>> topList(Information information, int root_unit_id) {
        return appInformationDao.topList(information, root_unit_id);
    }

    /**
     * 获取资讯信息列表
     *
     * @param pageBean
     * @param information
     * @return
     */
    public PageBean list(PageBean pageBean, Information information, int root_unit_id) {
        return appInformationDao.list(pageBean, information, root_unit_id);
    }


    /**
     * 单篇列表
     * @param pageBean
     * @param user_id
     * @return
     */
    public PageBean singleList(PageBean pageBean, Integer user_id, int root_unit_id){
        return appInformationDao.singleList(pageBean, user_id, root_unit_id);
    }

    /**
     * 单篇列表
     * @param pageBean
     * @param user_id
     * @return
     */
    public PageBean collectList(PageBean pageBean, Integer user_id, int root_unit_id){
        return appInformationDao.collectList(pageBean, user_id, root_unit_id);
    }

    /**
     * 单篇列表
     * @param pageBean
     * @param user_id
     * @return
     */
    public PageBean historyList(PageBean pageBean, Integer user_id, int root_unit_id){
        return appInformationDao.historyList(pageBean, user_id, root_unit_id);
    }

    /**
     * 添加评论
     * @param informationComment
     */
    @Transactional
    public void addComment(InformationComment informationComment,MemberComment memberComment){
        appInformationCommentService.save(informationComment);
        if(memberComment != null){
            appMemberCommentService.save(memberComment);
            updateCommentNum(informationComment.getInformation_id(), 1);
        }
    }

    /**
     * 评论列表
     * @param pageBean
     * @param comment
     * @param uid
     * @return
     */
    public PageBean commentList(PageBean pageBean, InformationComment comment, Integer uid){
        pageBean = appInformationCommentService.list(pageBean, comment);
        if (pageBean.getMapList() != null && pageBean.getMapList().size() > 0) {
            List<Map<String, Object>> list = pageBean.getMapList();
            if (uid != null) {
                for (Map<String, Object> map : list) {
                    Integer comment_id = (Integer) map.get("id");
                    // 判断是否点赞过这条
                    if (comment_id != null && appInformationUpService.isUped(comment_id, uid)) {
                        map.put("is_up", 1);
                    } else {
                        map.put("is_up", 0);
                    }
                }
            }
        }
        return pageBean;
    }

    /**
     * 获取资讯详细信息
     *
     * @param id
     * @param unit_id
     * @return
     */
    public Map<String, Object> get(Integer id, Integer unit_id, Integer uid) {
        Map<String, Object> bean = appInformationDao.get(id, null);
        PageBean pb = new PageBean();
        if (bean != null) {
            InformationComment comment = new InformationComment();
            comment.setInformation_id(id);
            pb = commentList(pb, comment, uid);
            bean.put("comment", PageBean.transformListMap(pb));
            if (uid != null && uid > 0) {
                bean.put("collection", appMemberCollectionService.isCollected(uid, id, GlobalValue.BusinessType.INFORMATION));
                InformationMotion motion = appInformationMotionService.getInformationMotion(id, uid);
                if(motion != null){
                    bean.put("user_motion", motion.getMotion());
                }
                bean.put("focus",appMemberFocusService.isFocus(Integer.valueOf(bean.get("unit_id").toString()), uid));
            }
            //获取收藏数量
            bean.put("collect_num",appMemberCollectionService.count(id, GlobalValue.BusinessType.INFORMATION));
        }
        return bean;
    }

    /**
     * 更新当前资讯阅读数
     * @param infoId
     * @param unit_id
     */
    @Transactional
    public void updateReading(Integer infoId, Integer unit_id) {
        appInformationDao.updateReading(infoId, unit_id);
    }

    /**
     * 更新当前资讯评论数
     * @param infoId
     */
    @Transactional
    public void updateCommentNum(Integer infoId, int num) {
        appInformationDao.updateCommentNum(infoId, num);
    }

    /**
     * 保存热点评论
     * @param motion
     */
    @Transactional(readOnly = false)
    public void saveMotion(int id, int user_id,String motion) {
        InformationMotion informationMotion = new InformationMotion();
        informationMotion.setCreate_time(new Date());
        informationMotion.setInformation_id(id);
        informationMotion.setMember_id(user_id);
        informationMotion.setMotion(motion);
        appInformationMotionService.save(informationMotion);
        Map<String,Object> mo = appInformationMotionService.getMostMotion(id);
        updateMotion(id, (String) mo.get("motion"));

    }

    /**
     * 更新情绪
     * @param id
     * @param motion
     */
    @Transactional
    public void updateMotion(int id, String motion){
        appInformationDao.updateMotion(id, motion);
    }

    /**
     * 作者
     * @param unit_id
     * @return
     */
    public Map<String,Object> author(Integer unit_id,Integer user_id){
        Map<String,Object> map = unitService.get(unit_id);
        map.put("article_num", authorList(new PageBean(1), unit_id).getTotalCount());
        map.put("member_num", appMemberFocusService.focusNum(unit_id));
        if(user_id != null){
            map.put("focus",appMemberFocusService.isFocus(unit_id, user_id));
        }
        return map;
    }

    /**
     * 作者列表
     * @param unit_id
     * @return
     */
    public PageBean authorList(PageBean pageBean, Integer unit_id){
        Information information = new Information();
        information.setUnit_id(unit_id);
        information.setPublish_status(GlobalValue.PublishStatus.PUBLISHED);
        return appInformationDao.list(pageBean, information, unitService.getRootUnit(unit_id));
    }
}
